﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using Wicresoft.Task.Common;
using Wicresoft.Task.Common.Entity;
using Wicresoft.Task.Team.Entity;

namespace Wicresoft.Task.Team
{
    public class ShelvesetNofitier : Wicresoft.Task.Scheduler.Task
    {
        protected override void Execute()
        {
            try
            {
                TfsAutoNotifyContext TfsDB = new TfsAutoNotifyContext();
                Nofitier notify = new Nofitier(this.Bag.Website,this.Bag.CC);
                // 从数据库读取Shelvesets，然后发邮件
                ShelveInfo[] shelvesets = null;
                if (TfsDB.ShelveInfoes != null)
                {
                    if (TfsDB.ShelveInfoes.Where(p => !p.IsCheck) != null)
                    {
                        if (TfsDB.ShelveInfoes.Where(p => !p.IsCheck).Count() != 0)
                        {
                            shelvesets = TfsDB.ShelveInfoes.Where(p => !p.IsCheck).ToArray();
                        }
                    }
                }
                if (shelvesets != null)
                {
                    foreach (var item in shelvesets)
                    {
                        EmailTemplate email = notify.CreateEmail(item);
                        if (email != null)
                        {
                            //Logger.Log.Info(email.To,email.Subject);
                            SmtpHelper.SendEmail(email.Subject, email.Body, email.From, SplitStr(';', email.To).ToArray(), SplitStr(';', email.Cc).ToArray(), SplitStr(';', email.Bcc).ToArray(), SplitStr(';', email.ReplyTo).ToArray());
                            ShelveInfo x = TfsDB.ShelveInfoes.First(p => p.Name == item.Name && p.OwnerName == item.OwnerName && p.CreationDate == item.CreationDate);
                            x.IsCheck = true;
                            x.CodeReviewer = TfsDB.EmployeesInfoes.First(p => p.Email == email.To).WSAlias;
                            TfsDB.SaveChanges();

                        }
                        else
                        {
                            ShelveInfo x = TfsDB.ShelveInfoes.First(p => p.Name == item.Name && p.OwnerName == item.OwnerName && p.CreationDate == item.CreationDate);
                            x.IsCheck = true;
                            TfsDB.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Log.Exception(this.Name, e);
            }
        }
        public static List<string> SplitStr(char a, string str)
        {
            List<string> strList = new List<string>();
            if (str == null)
            {
                strList = null;
            }
            else
            {
                string[] _x = str.Split(new char[] { a });

                foreach (var _y in _x)
                {
                    if (_y != null)
                    {
                        strList.Add(_y);
                    }
                }
            }

            return strList;
        }

    }
}
